package com.ruoyi.core.service.impl;

import java.util.List;

import com.ruoyi.common.entity.CoreEntity.ShippingAddresses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.core.mapper.ShippingAddressesMapper;

import com.ruoyi.core.service.IShippingAddressesService;

/**
 * 用户收货地址Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Service
public class ShippingAddressesServiceImpl implements IShippingAddressesService 
{
    @Autowired
    private ShippingAddressesMapper shippingAddressesMapper;

    // 在现有的 ShippingAddressesServiceImpl 类中添加以下方法

    @Override
    public List<ShippingAddresses> getUserAddressesByUserId(Long userId) {
        ShippingAddresses query = new ShippingAddresses();
        query.setUserId(userId);
        return shippingAddressesMapper.selectShippingAddressesList(query);
    }

    @Override
    public int clearDefaultByUserId(Long userId) {
        return shippingAddressesMapper.clearDefaultByUserId(userId);
    }

    @Override
    public int setDefaultAddress(Long addressId, Long userId) {
        // 1. 验证地址是否属于该用户
        ShippingAddresses address = selectShippingAddressesById(addressId);
        if (address == null || !address.getUserId().equals(userId)) {
            throw new RuntimeException("地址不存在或无权限操作");
        }

        // 2. 清除该用户所有地址的默认状态
        clearDefaultByUserId(userId);

        // 3. 设置指定地址为默认
        return shippingAddressesMapper.setDefaultById(addressId);
    }

    /**
     * 查询用户收货地址
     * 
     * @param id 用户收货地址主键
     * @return 用户收货地址
     */
    @Override
    public ShippingAddresses selectShippingAddressesById(Long id)
    {
        return shippingAddressesMapper.selectShippingAddressesById(id);
    }

    /**
     * 查询用户收货地址列表
     * 
     * @param shippingAddresses 用户收货地址
     * @return 用户收货地址
     */
    @Override
    public List<ShippingAddresses> selectShippingAddressesList(ShippingAddresses shippingAddresses)
    {
        return shippingAddressesMapper.selectShippingAddressesList(shippingAddresses);
    }

    /**
     * 新增用户收货地址
     * 
     * @param shippingAddresses 用户收货地址
     * @return 结果
     */
    @Override
    public int insertShippingAddresses(ShippingAddresses shippingAddresses)
    {
        return shippingAddressesMapper.insertShippingAddresses(shippingAddresses);
    }

    /**
     * 修改用户收货地址
     * 
     * @param shippingAddresses 用户收货地址
     * @return 结果
     */
    @Override
    public int updateShippingAddresses(ShippingAddresses shippingAddresses)
    {
        return shippingAddressesMapper.updateShippingAddresses(shippingAddresses);
    }

    /**
     * 批量删除用户收货地址
     * 
     * @param ids 需要删除的用户收货地址主键
     * @return 结果
     */
    @Override
    public int deleteShippingAddressesByIds(Long[] ids)
    {
        return shippingAddressesMapper.deleteShippingAddressesByIds(ids);
    }

    /**
     * 删除用户收货地址信息
     * 
     * @param id 用户收货地址主键
     * @return 结果
     */
    @Override
    public int deleteShippingAddressesById(Long id)
    {
        return shippingAddressesMapper.deleteShippingAddressesById(id);
    }


}
